package com.atgm.mall.gmmailcoupon.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.atgm.common.utils.GmMallUtils;
import com.atgm.common.utils.PageUtils;
import com.atgm.common.utils.Query;
import com.atgm.common.utils.R;
import com.atgm.mall.gmmailcoupon.dao.PeriGoodsSaleDao;
import com.atgm.mall.gmmailcoupon.entity.PeriGoodsSaleEntity;
import com.atgm.mall.gmmailcoupon.feign.PeriFeignService;
import com.atgm.mall.gmmailcoupon.feign.vo.FeignPeriGoodsEntity;
import com.atgm.mall.gmmailcoupon.service.PeriGoodsSaleService;
import com.atgm.mall.gmmailcoupon.vo.resp.PeriGoodsSaleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class PeriGoodsSaleServiceImpl extends ServiceImpl<PeriGoodsSaleDao, PeriGoodsSaleEntity> implements PeriGoodsSaleService {

    @Autowired
    @Qualifier("periFeignService")
    private PeriFeignService periFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        List<PeriGoodsSaleVo> periGoodsSaleVoList = new ArrayList<>();
        IPage<PeriGoodsSaleEntity> page = this.page(new Query<PeriGoodsSaleEntity>().getPage(params),
                new QueryWrapper<PeriGoodsSaleEntity>());
        PageUtils pageUtils = new PageUtils(page);

        List<PeriGoodsSaleEntity> periGoodsSaleList = page.getRecords();

        //获取周边商品id
        if (GmMallUtils.isNotEmpty(periGoodsSaleList)) {
            periGoodsSaleList.forEach(periGoodsSaleEntity -> {
                PeriGoodsSaleVo periGoodsSaleVo = new PeriGoodsSaleVo();
                BeanUtils.copyProperties(periGoodsSaleEntity, periGoodsSaleVo);
                periGoodsSaleVoList.add(periGoodsSaleVo);
            });
            List<Long> periGoodsSaleIdList = periGoodsSaleList.stream().map(PeriGoodsSaleEntity::getPeriGoodsId).collect(Collectors.toList());
            R r = periFeignService.getPeriGoodsInfoByIds(periGoodsSaleIdList);
            if (0 == r.getCode()) {
                List<FeignPeriGoodsEntity> data = r.getData(new TypeReference<List<FeignPeriGoodsEntity>>() {
                });
                if (GmMallUtils.isNotEmpty(data)) {
                    periGoodsSaleVoList.forEach(periGoodsSaleVo -> {
                        //名称
                        periGoodsSaleVo.setPeriGoodsName(data.stream().filter(feignPeriGoodsEntity -> {
                            return feignPeriGoodsEntity.getId().equals(periGoodsSaleVo.getPeriGoodsId());
                        }).findAny().get().getName());
                        //默认图片
                        periGoodsSaleVo.setDefImg(data.stream().filter(feignPeriGoodsEntity -> {
                            return feignPeriGoodsEntity.getId().equals(periGoodsSaleVo.getPeriGoodsId());
                        }).findAny().get().getDefImg());
                        //原价
                        periGoodsSaleVo.setPrice(data.stream().filter(feignPeriGoodsEntity -> {
                            return feignPeriGoodsEntity.getId().equals(periGoodsSaleVo.getPeriGoodsId());
                        }).findAny().get().getPrice());
                    });
                }
            }
            pageUtils.setList(periGoodsSaleVoList);
        }else {
            pageUtils.setList(null);
        }
        return pageUtils;
    }

    @Override
    public List<PeriGoodsSaleEntity> queryPeriGoodsSaleByPeriGoodsIds(List<Long> ids) {
        QueryWrapper<PeriGoodsSaleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("peri_goods_id", ids);
        List<PeriGoodsSaleEntity> list = baseMapper.selectList(queryWrapper);
        return list;
    }
}
